<HTML>
<HEAD>
<TITLE>[Chapter 10] 10.7 MenuBar</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:46:34 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch10_06.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 10<br>Would You Like to Choose from the Menu?</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch10_08.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-10-SECT-7">10.7 MenuBar</A></h2>

<P CLASS=para>
<A NAME="CH10.MENU3"></A><A NAME="CH10.MENU4"></A>The <tt CLASS=literal>MenuBar</tt> is the component 
you add to the <tt CLASS=literal>Frame</tt> that is 
displayed on the top line of the <tt CLASS=literal>Frame</tt>; 
the <tt CLASS=literal>MenuBar</tt> contains menus. 
A <tt CLASS=literal>Frame</tt> can display only one 
<tt CLASS=literal>MenuBar</tt> at a time. However, 
you can change the <tt CLASS=literal>MenuBar</tt> 
based on the state of the program so that different menus can appear at 
different points. The <tt CLASS=literal>MenuBar</tt> 
class extends <tt CLASS=literal>MenuComponent</tt> 
and implements the <tt CLASS=literal>MenuContainer</tt> 
interface. 

<P CLASS=para>
A <tt CLASS=literal>MenuBar</tt> can be used only 
as a child component of a <tt CLASS=literal>Frame</tt>. 
An applet cannot have a <tt CLASS=literal>MenuBar</tt> 
attached to it, unless you implement the whole thing yourself. Normally, 
you cannot modify the <tt CLASS=literal>MenuBar</tt> 
of the applet holder (the browser), unless it is Java based. In other 
words, you cannot affect the menus of Netscape Navigator, but you can customize 
<I CLASS=emphasis>appletviewer</I> and HotJava, as shown in the following code with the result 
shown in <A HREF="ch10_07.htm#JAWT-CH-10-FIG-4">Figure 10.4</A>. The <tt CLASS=literal>getTopLevelParent()</tt> 
method was introduced in <A HREF="ch06_04.htm#JAWT-CH-6-SECT-4">Window</A> with <tt CLASS=literal>Window</tt>. 

<DIV CLASS=screen>
<P>
<PRE>
import java.awt.*;
public class ChangeMenu extends java.applet.Applet {
    public void init ()  {
        Frame f = ComponentUtilities.getTopLevelParent(this);
        if (f != null) {
            MenuBar mb = f.getMenuBar();
            Menu m = new Menu ("Cool");
            mb.add (m);
        }
    }
}
</PRE>
</DIV>

<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="JAWT-CH-10-FIG-4">Figure 10.4: Customizing appletviewer's MenuBar</A></h4>


<p>
<img align=middle src="./figs/jawt1004.gif" alt="[Graphic: Figure 10-4]" width=503 height=136 border=0>

</DIV>

<DIV CLASS=note>
<P CLASS=note><BLOCKQUOTE><P><B>NOTE:</B> 
</blockquote><P>
</DIV>

<P CLASS=para>
When you add a <tt CLASS=literal>MenuBar</tt> 
to a <tt CLASS=literal>Frame</tt>, it takes up space 
that is part of the drawing area. You need to get the top insets to find 
out how much space is occupied by the <tt CLASS=literal>MenuBar</tt> 
and be careful not to draw under it. If you do, the <tt CLASS=literal>MenuBar</tt> 
will cover what you draw. 
</blockquote><P>
</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-10-SECT-7.1">MenuBar Methods</A></h3>Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public MenuBar() </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>MenuBar</tt> constructor creates 
an empty <tt CLASS=literal>MenuBar</tt>. To add menus 
to the <tt CLASS=literal>MenuBar</tt>, use the <tt CLASS=literal>add()</tt>method. </DL>
Menus

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public int getMenuCount () <img src="gifs/bstar.gif" alt="(New)" border=0></I><br><I CLASS=emphasis>public int countMenus () <img src="gifs/wstar.gif" alt="(Deprecated)" border=0></I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getMenuCount()</tt> method returns 
the number of top-level menus within the <tt CLASS=literal>MenuBar</tt>. 

<P CLASS=para>
<tt CLASS=literal>countMenus()</tt> is the Java 1.0 
name for this method. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Menu getMenu (int index) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getMenu()</tt> method returns 
the <tt CLASS=literal>Menu</tt> at position <tt CLASS=literal>index</tt>. 
If <tt CLASS=literal>index</tt> is invalid, <tt CLASS=literal>getMenu()</tt> 
throws the run-time exception <tt CLASS=literal>ArrayIndexOutOfBoundsException</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized Menu add (Menu m) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>add()</tt> method puts choice 
<tt CLASS=literal>m</tt> on the <tt CLASS=literal>MenuBar</tt>. 
The label used to create <tt CLASS=literal>m</tt> 
is displayed on the <tt CLASS=literal>MenuBar</tt>. 
If <tt CLASS=literal>m</tt> is already in another 
<tt CLASS=literal>MenuBar</tt>, it is removed from 
it. The order of items added determines the order displayed on the <tt CLASS=literal>MenuBar</tt>, 
with one exception: if a menu is designated as a help menu by <tt CLASS=literal>setHelpMenu()</tt>, 
it is placed at the right end of the menu bar. Only a <tt CLASS=literal>Menu</tt> 
can be added to a <tt CLASS=literal>MenuBar</tt>; 
you can't add a <tt CLASS=literal>MenuItem</tt>. 
In other words, a <tt CLASS=literal>MenuItem</tt> has 
to lie under at least one menu. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void remove (int index) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>remove()</tt> method removes 
the <tt CLASS=literal>Menu</tt> at position <tt CLASS=literal>index</tt> 
from the <tt CLASS=literal>MenuBar</tt>. If <tt CLASS=literal>index</tt> 
is invalid, <tt CLASS=literal>remove()</tt> throws 
the <tt CLASS=literal>ArrayIndexOutOfBoundsException</tt> run-time exception. 
<tt CLASS=literal>index</tt> is zero based. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void remove (MenuComponent component) </I><br>
<DD>

<P CLASS=para>
This version of <tt CLASS=literal>remove()</tt> removes 
the menu <tt CLASS=literal>component</tt> from the 
<tt CLASS=literal>MenuBar</tt>. If <tt CLASS=literal>component</tt> 
is not in <tt CLASS=literal>MenuBar</tt>, nothing 
happens. The system calls this method when you add a new <tt CLASS=literal>Menu</tt> 
to make sure it does not exist on another <tt CLASS=literal>MenuBar</tt>. </DL>
Shortcuts

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public MenuItem getShortcutMenuItem (MenuShortcut shortcut) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getShortcutMenuItem()</tt> method 
retrieves the <tt CLASS=literal>MenuItem</tt> associated 
with the <tt CLASS=literal>MenuShortcut</tt> <tt CLASS=literal>shortcut</tt>. 
If <tt CLASS=literal>MenuShortcut</tt> does not exist 
for this <tt CLASS=literal>Menu</tt>, the method returns 
<tt CLASS=literal>null</tt>. <tt CLASS=literal>getShortcutMenuItem()</tt> 
walks through the all submenus recursively to try to find <tt CLASS=literal>shortcut</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized Enumeration shortcuts() <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>shortcuts()</tt> method retrieves 
an <tt CLASS=literal>Enumeration</tt> of all the <tt CLASS=literal>MenuShortcut</tt> 
objects associated with this <tt CLASS=literal>MenuBar</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void deleteShortcut (MenuShortcut shortcut) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>deleteShortcut()</tt> method 
removes <tt CLASS=literal>MenuShortcut</tt> from the 
associated <tt CLASS=literal>MenuItem</tt> in the 
<tt CLASS=literal>MenuBar</tt>. If the shortcut is 
not associated with any menu item, nothing happens. </DL>
Help menus

<P CLASS=para>
It is the convention on many platforms to display help menus as the last 
menu on the <tt CLASS=literal>MenuBar</tt>. The <tt CLASS=literal>MenuBar</tt> 
class lets you designate one of the menus as this special menu. The physical 
position of a help menu depends on the platform, but those giving special treatment to help menus place them on the right. A <tt CLASS=literal>Menu</tt> 
designated as a help menu doesn't have to bear the label "Help"; 
the label is up to you. 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Menu getHelpMenu () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getHelpMenu()</tt> method returns 
the <tt CLASS=literal>Menu</tt> that has been designated 
as the help menu with <tt CLASS=literal>setHelpMenu()</tt>. 
If the menu bar doesn't have a help menu, <tt CLASS=literal>getHelpMenu()</tt> 
returns <tt CLASS=literal>null</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void setHelpMenu (Menu m) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setHelpMenu()</tt> method sets 
the menu bar's help menu to <tt CLASS=literal>m</tt>. 
This makes <tt CLASS=literal>m</tt> the rightmost 
menu on the <tt CLASS=literal>MenuBar</tt>, possibly 
right justified. If <tt CLASS=literal>m</tt> is not 
already on the <tt CLASS=literal>MenuBar</tt>, nothing 
happens. </DL>
Peers

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void addNotify ()  </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>addNotify()</tt> method creates 
the <tt CLASS=literal>MenuBar</tt> peer with 
all the menus on it, and in 
turn their menu items. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void removeNotify ()  </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>removeNotify()</tt> method destroys 
the peer of the <tt CLASS=literal>MenuBar</tt> and 
removes it from the screen. The peers of the items on the <tt CLASS=literal>MenuBar</tt> 
are also destroyed. </DL>
</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-10-SECT-7.2">MenuBar Events</A></h3>

<P CLASS=para>
A <tt CLASS=literal>MenuBar</tt> does not generate any events. 

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch10_06.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch10_08.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>CheckboxMenuItem</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Putting It All Together</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
